package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.INewsGroupDAO;
import org.jsola.hr.dto.NewsGroupAddDTO;
import org.jsola.hr.dto.NewsGroupUpdateDTO;
import org.jsola.hr.entity.NewsGroupDO;
import org.jsola.hr.query.NewsGroupQuery;
import org.jsola.hr.service.INewsGroupService;
import org.jsola.hr.vo.NewsGroupListVO;
import org.jsola.hr.vo.NewsGroupVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 官网新闻类型分组
 *
 * @author zhr
 */
@Service("hrNewsGroupServiceImpl")
public class NewsGroupServiceImpl implements INewsGroupService {

    @Autowired
    private INewsGroupDAO newsGroupDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public NewsGroupVO save(NewsGroupAddDTO newsGroupAddDTO, TokenUser tokenUser) {
        // 转DO
        NewsGroupDO newsGroupDO = newsGroupAddDTO.to(NewsGroupDO.class);
        // 保存
        newsGroupDO = save(newsGroupDO, "default", tokenUser.getUserId());
        return newsGroupDO.to(NewsGroupVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(NewsGroupUpdateDTO newsGroupUpdateDTO, TokenUser tokenUser) {
        //转DO
        NewsGroupDO newsGroupDO = newsGroupUpdateDTO.to(NewsGroupDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(newsGroupDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... newsGroupIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) newsGroupIds);
    }


    @Override
    public NewsGroupVO selectById(String newsGroupId, String siteId) {
        NewsGroupDO newsGroupDO = selectDOById(newsGroupId, siteId);
        if (newsGroupDO == null) {
            return null;
        }
        return newsGroupDO.to(NewsGroupVO.class);
    }

    @Override
    public List<NewsGroupListVO> select(NewsGroupQuery newsGroupQuery, String siteId) {
        List<NewsGroupDO> newsGroupDOList = selectDO(newsGroupQuery, siteId);
        if(CollectionUtils.isEmpty(newsGroupDOList)) {
            return newsGroupDOList == null ? null : new ArrayList<>();
        }
        return newsGroupDOList.stream()
                .map(newsGroupDO -> newsGroupDO.to(NewsGroupListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(NewsGroupQuery newsGroupQuery, String siteId) {
        Example example = buildExample(newsGroupQuery,siteId);
        return newsGroupDAO.selectCountByExample(example);
    }

    @Override
    public Page<NewsGroupListVO> selectPage(NewsGroupQuery newsGroupQuery, String siteId) {
        Example example = buildExample(newsGroupQuery,siteId);
        Page<NewsGroupDO> page = newsGroupDAO.selectPageByExample(example,
                newsGroupQuery.getPageNo(),
                newsGroupQuery.getPageSize());

        return page.to(NewsGroupListVO.class);
    }

    @Override
    public NewsGroupDO selectDOById(String newsGroupId, String siteId) {
        return listById(newsGroupId, "default");
    }

    @Override
    public List<NewsGroupDO> selectDO(NewsGroupQuery newsGroupQuery, String siteId) {
        Example example = buildExample(newsGroupQuery,siteId);
        return newsGroupDAO.selectByExample(example);
    }

    @Override
    public String selectGroupByCode(String code) {
        return newsGroupDAO.selectGroupByCode(code);
    }


    /**
     * 根据查询参数，构建example

     * @param newsGroupQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(NewsGroupQuery newsGroupQuery, String siteId) {
        Example example = new Example(NewsGroupDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", "default");
        if (newsGroupQuery.getCode() != null) {
            example.and().andEqualTo("code", newsGroupQuery.getCode());
        }
        if (newsGroupQuery.getName() != null) {
            example.and().andEqualTo("name", newsGroupQuery.getName());
        }
        if (newsGroupQuery.getType() != null) {
            example.and().andEqualTo("type", newsGroupQuery.getType());
        }
        // 排序
        ExampleKit.setExampleOrder(example,newsGroupQuery.getOrders());
        return example;
    }
}




